package cn.jeeweb.web.modules.equipment.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenanceCheckHistory;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenancePlan;
import cn.jeeweb.web.modules.equipment.mapper.EqsMaintenancePlanMapper;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceCheckHistoryService;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenancePlanService;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceTaskService;
import cn.jeeweb.web.modules.sys.entity.CodeTable;
import cn.jeeweb.web.modules.sys.service.ICodeTableService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @description: eqs_maintenance_plan服务实现
*/
@Transactional
@Service("eqsMaintenancePlanService")
public class EqsMaintenancePlanServiceImpl extends CommonServiceImpl<EqsMaintenancePlanMapper,EqsMaintenancePlan> implements IEqsMaintenancePlanService {

    @Autowired
    ICodeTableService codeTableService;

    @Autowired
    IEqsMaintenanceCheckHistoryService eqsMaintenanceCheckHistoryService;

    @Autowired
    IEqsMaintenanceTaskService eqsMaintenanceTaskService;

    public boolean insert(String json){
        EqsMaintenancePlan eqsMaintenancePlan = JSON.parseObject(json,EqsMaintenancePlan.class);
        CodeTable codeTable = new CodeTable();
        codeTable.setCode("MTPAUTOCODE");
        eqsMaintenancePlan.setCode(codeTableService.createNewCode(codeTable));
        return insert(eqsMaintenancePlan);
    }

    public EqsMaintenancePlan insertAndSelect(String json,String json_){
        EqsMaintenancePlan eqsMaintenancePlan = JSON.parseObject(json, EqsMaintenancePlan.class);
        EqsMaintenanceCheckHistory eqsMaintenanceCheckHistory = JSON.parseObject(json_, EqsMaintenanceCheckHistory.class);
        eqsMaintenanceCheckHistory = eqsMaintenanceCheckHistoryService.initEqsMaintenanceCheckHistory_(eqsMaintenanceCheckHistory);
        boolean result = eqsMaintenanceCheckHistoryService.insert(eqsMaintenanceCheckHistory);
        if(result){
            eqsMaintenancePlan.setCheckHistoryId(eqsMaintenanceCheckHistory.getId());
        }
        return super.insertAndSelect(eqsMaintenancePlan);
    }


    public boolean insertOrUpdate(String json,String json_){
        EqsMaintenancePlan eqsMaintenancePlan = JSON.parseObject(json, EqsMaintenancePlan.class);
        EqsMaintenanceCheckHistory eqsMaintenanceCheckHistory = JSON.parseObject(json_, EqsMaintenanceCheckHistory.class);
        eqsMaintenanceCheckHistory = eqsMaintenanceCheckHistoryService.initEqsMaintenanceCheckHistory_(eqsMaintenanceCheckHistory);
        if(StringUtils.isNotBlank(eqsMaintenanceCheckHistory.getId())){
            eqsMaintenanceCheckHistoryService.insertOrUpdate(eqsMaintenanceCheckHistory);
        }else{
            eqsMaintenanceCheckHistoryService.insert(eqsMaintenanceCheckHistory);
            eqsMaintenancePlan.setCheckHistoryId(eqsMaintenanceCheckHistory.getId());
        }
        return insertOrUpdate(eqsMaintenancePlan);
    }
}